package com.yyjj.zixun.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.yyjj.zixun.R;
import com.yyjj.zixun.adapter.ArticleAdapter;
import com.yyjj.zixun.entity.ArticleBean;
import com.yyjj.zixun.entity.ArticleDetailsBean;
import com.yyjj.zixun.server.NewsBack;
import com.yyjj.zixun.server.NewsGlobal;

import java.util.List;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.utils.FastUtil;
import tech.com.commoncore.utils.ImageGetterUtils;

import static com.yyjj.zixun.ZiXunPath.NEWS_ARTICLE_DETAIL;
import static com.yyjj.zixun.ZiXunPath.NEWS_ARTICLE_KEY;

@Route(path = NEWS_ARTICLE_DETAIL)
public class ArticleDetailsActivity extends BaseTitleActivity implements View.OnClickListener {

    private TextView titleTx, dateTx, contentTx;

    private RecyclerView newsRecycler;
    private ArticleAdapter articleAdapter;

    private int articleId;

    private ArticleDetailsBean detailsBean;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("资讯详情")
//                .setRightTextDrawable(R.drawable.icon_default_share)
                .setOnRightTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        handlerShearClick();
                    }
                });
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_article_details;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        titleTx = findViewById(R.id.article_detail_title_tx);
        dateTx = findViewById(R.id.article_detail_date_tx);
        contentTx = findViewById(R.id.article_detail_content_tx);
        newsRecycler = findViewById(R.id.article_recycler);

        articleAdapter = new ArticleAdapter(R.layout.news_layout_item_article);
        newsRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        newsRecycler.setAdapter(articleAdapter);

        articleId = getIntent().getIntExtra(NEWS_ARTICLE_KEY, 0);

        requestNewsDetail();
    }

    @Override
    public void onClick(View v) {
    }

    private void handlerShearClick() {
        String conteng = "";
        if (detailsBean != null)
            conteng = detailsBean.getPost_content();
        FastUtil.startShareText(mContext, conteng);
    }

    private void requestNewsDetail() {
        showLoading();
        if (articleId == 0)
            return;
        NewsGlobal.getNewsImp().requestArticleDetails(articleId, new NewsBack<ArticleDetailsBean>() {
            @Override
            public void onBack(ArticleDetailsBean bean, String err) {
                hideLoading();
                if (bean != null) {
                    detailsBean = bean;
                    updateDataView(bean.getPost_title(), bean.getCreate_time(), bean.getPost_content());
                }
            }
        });

        NewsGlobal.getNewsImp().requestArticle(3, 5, "id", "195", new NewsBack<List<ArticleBean>>() {
            @Override
            public void onBack(List<ArticleBean> beans, String err) {
                if (beans != null && !beans.isEmpty()) {
                    List<ArticleBean> showLike;
                    if (beans.size() > 10) {
                        showLike = beans.subList(0, 10);
                    } else {
                        showLike = beans;
                    }
                    articleAdapter.replaceData(showLike);
                    articleAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    private void updateDataView(String title, String date, String content) {
        titleTx.setText(title);
        dateTx.setText(date);
        contentTx.setText(Html.fromHtml(content, new ImageGetterUtils.MyImageGetter(this, contentTx), null));
    }
}
